<template>

	<view>

		<view class="imgg">
			<image src="../../static/images/logo1.png" mode=""></image>
		</view>

		<view class="content">

			<u--form>

				<u-input class="inp u-border" v-model="phone" placeholder="请输入手机号">

				</u-input>

			</u--form>


			<view class="u-demo-block__content" style="margin-top: 15px;">
				<!-- 注意：由于兼容性差异，如果需要使用前后插槽，nvue下需使用u--input，非nvue下需使用u-input -->
				<!-- #ifndef APP-NVUE -->
				<u-input v-model="code" placeholder="请输入验证码">
					<!-- #endif -->
					<!-- #ifdef APP-NVUE -->
					<u--input placeholder="请输入验证码">
						<!-- #endif -->
						<template slot="suffix">
							<u-code ref="uCode" @change="codeChange" seconds="6" changeText="X秒重新获取"></u-code>
							<u-button @tap="getCode" :text="tips" type="success"></u-button>
						</template>
						<!-- #ifndef APP-NVUE -->
				</u-input>
				<!-- #endif -->
				<!-- #ifdef APP-NVUE -->
				</u--input>
				<!-- #endif -->
			</view>


			<u-button type="success" @click="login" class="btn">点击登录</u-button>


		</view>



	</view>
</template>

<script>
	import {
		getsms,
		login
	} from '../../rquest/api.js'
	export default {
		data() {
			return {
				tips: '发送验证码',
				value: '',
				code: '',
				phone: '',
				apicode: ''

			}
		},
		watch: {
			value(newValue, oldValue) {
				// console.log('v-model', newValue);
			}
		},
		methods: {
			codeChange(text) {
				this.tips = text;
			},

			getCode() {

				let reg = /^1[3-9]\d{9}$/
				if (!reg.test(this.phone)) {
					uni.showToast({
						title: '请输入正确的手机号'
					})
					return
				}

				getsms({
					phone: this.phone
				}).then(res => {
					console.log(res);
					this.apicode = res.data.list.code
					uni.setStorageSync('apicode', this.apicode)
				})



				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					setTimeout(() => {
						uni.hideLoading();
						// 这里此提示会被this.start()方法中的提示覆盖
						uni.$u.toast('验证码已发送');
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
					}, 2000);
				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			},
			change(e) {
				console.log('change', e);
			},
			login() {
				let lowcode = ''
				let reg = /^1[3-9]\d{9}$/
				if (!reg.test(this.phone)) {
					uni.showToast({
						title: '请输入正确的手机号'
					})
					return
				}
				if (uni.getStorageSync('apicode')) {
					lowcode = uni.getStorageSync('apicode')
				}
				if (lowcode != this.code) {
					uni.showToast({
						title: "请输入正确的验证码"
					})
					return
				} 
				uni.showToast({
					title: "登录成功"
				})
				
				login({
					phone:this.phone
				}).then(res=>{
					console.log(res);
				if(res.data.code==200){
					let user = res.data.list
					uni.setStorageSync('user',user)
						uni.switchTab({
						url: '../home/home'
					})
				}
				
				})
				

			}
		}
	}
</script>

<style>
	.imgg {
		text-align: center;
		margin-top: 200rpx;
		margin-bottom: 69rpx;
	}

	.imgg image {
		width: 60%;
		height: 120rpx;

	}

	.btn {
		margin-top: 60rpx;
	}

	.content {
		overflow: hidden;
		width: 90%;
		height: auto;
		margin: auto;
	}

	.inp {
		height: 50px !important;
	}

	.u-border {
		border-width: 0.5px !important;
		border-color: #dadbde !important;
		/* border-style: solid; */
		border: none !important;
		border-bottom: solid !important;
		border-width: 0.5px !important;
		border-color: #dadbde !important;
		border-radius: 0px !important;
	}
</style>
